<template>
    <div>
        <div style="border: 1px solid #ccc; margin-top: 10px; z-index: 100">
            <Toolbar
                :editor="editorRef"
                :defaultConfig="toolbarConfig"
                style="border-bottom: 1px solid #ccc"
            />
            <Editor
                :defaultConfig="editorConfig"
                v-model="value"
                style="height: 400px; overflow-y: hidden"
                @onCreated="handleCreated"
                @onChange="handleChange"
            />
        </div>
    </div>
</template>
<script>
export default {
    name: 'BasiEditor'
}
</script>
<script setup>
import '@wangeditor/editor/dist/css/style.css'
import { onBeforeUnmount, shallowRef, defineProps, defineEmits } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
const props = defineProps({
    value: {
        type: String,
        default: ''
    }
})
const emit = defineEmits(['update:value'])
// 编辑器实例，必须用 shallowRef，重要！
const editorRef = shallowRef()
const toolbarConfig = {}
const editorConfig = {
    placeholder: '请输入内容...'
}

// 编辑器回调函数
const handleCreated = (editor) => {
    editorRef.value = editor // 记录 editor 实例，重要！
}
//改变函数
const handleChange = (editor) => {
    editor.getText()
    emit('update:value', editor.getHtml())
}
// 组件销毁时，也及时销毁编辑器，重要！
onBeforeUnmount(() => {
    const editor = editorRef.value
    if (editor == null) return
    editor.destroy()
})
</script>
